package com.haust.leyou.controller;

import com.haust.leyou.pojo.SpecGroup;
import com.haust.leyou.pojo.SpecParam;
import com.haust.leyou.service.SpecificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @Auther: csp1999
 * @Date: 2020/12/10/20:35
 * @Description:
 */
@Controller
@RequestMapping("/spec")
public class SpecificationController {

    @Autowired
    private SpecificationService specificationService;

    /**
     * 根据分类id查询规格组
     *
     * @param cid
     * @return
     */
    @GetMapping("/groups/{cid}")
    public ResponseEntity<List<SpecGroup>> queryGroupByCid(@PathVariable("cid") Long cid) {

        List<SpecGroup> groups = specificationService.queryGroupsByCid(cid);

        if (CollectionUtils.isEmpty(groups)) {
            return ResponseEntity.notFound().build();
        }

        return ResponseEntity.ok(groups);
    }

    /**
     * 根据gid,cid,generic,searching 查询规格参数
     *
     * @param gid       商品规格组gid
     * @param cid       商品分类cid
     * @param generic   是否是sku通用属性，true或false
     * @param searching 是否用于搜索过滤，true或false
     * @return
     */
    @GetMapping("/params")
    public ResponseEntity<List<SpecParam>> queryParams(
            @RequestParam(value = "cid", required = false) Long cid,
            @RequestParam(value = "gid", required = false) Long gid,
            @RequestParam(value = "generic", required = false) Boolean generic,
            @RequestParam(value = "searching", required = false) Boolean searching) {

        List<SpecParam> params = specificationService.queryParamsByGid(gid, cid, generic, searching);

        if (CollectionUtils.isEmpty(params)) {
            return ResponseEntity.notFound().build();
        }

        return ResponseEntity.ok(params);
    }

    /**
     * 根据cid 查询SpecGroup集合
     *
     * @param cid
     * @return
     */
    @GetMapping("/group/param/{cid}")
    public ResponseEntity<List<SpecGroup>> queryGroupsWithParam(@PathVariable("cid") Long cid) {
        List<SpecGroup> groups = specificationService.queryGroupsWithParam(cid);

        if (CollectionUtils.isEmpty(groups)) {
            return ResponseEntity.notFound().build();
        }

        return ResponseEntity.ok(groups);
    }
}
